<template>
	<view class="user_INFO">
    <u--form
        labelPosition="left"
        :model="from"
        :rules="rules"
        ref="form1"
    >
      <u-form-item  prop="password">
        <u--input placeholder="请设置密码" type="password" v-model="from.password" border="bottom" clearable
                  class='user_input'></u--input>
      </u-form-item>
      <u-form-item  prop="confirmPassword">
      <u--input placeholder="请确认密码" type="password" v-model="from.confirmPassword" border="bottom" clearable
                class='user_input'></u--input>
      </u-form-item>
    </u--form>



		<view class="next" @click="nextTo">
			注 册
		</view>
		<u-notify ref="uNotify" message="请检查表单内容"></u-notify>
		<u-toast ref="uToast" :show='show2'></u-toast>
	</view>
</template>

<script>
import {loginIn, register} from '../../api/api.js'
	export default {
		data() {
			return {
				from: {
					password: '',
					confirmPassword: ''
				},
				message: '',
				show: false,
				show2: false,
				option: null,
        rules:{
				  'password':{
            type: 'string',
            required: true,
            max: 20,
            min: 6,
            message: '输入6~20位密码',
            trigger: ['blur']
          },
          'confirmPassword':{
            type: 'string',
            max: 20,
            min: 6,
            required: true,
            message: '输入6~20位密码',
            trigger: ['blur']
          },
        }
			}
		},
		methods: {
			nextTo() {
				if (!!this.from.password && !!this.from.confirmPassword) {
					if (this.from.password !== this.from.confirmPassword) {
						this.message = '两次输入的密码不一致'
						this.$refs.uNotify.show({
							top: 10,
							type: 'warning',
							message: this.message,
							duration: 1000 * 2,
							fontSize: 20,
							safeAreaInsetTop: true
						})
					} else {
						register({...this.from,...this.option}).then((r)=>{
							if(r.code === 200){
								setTimeout((e)=>{
									uni.navigateTo({
										url:`../perfectINFO/perfectINFO?userId=${r.msg}&companyId=${this.option.companyId}&phonenumber=${this.option.phone}&username=${this.option.username}`
									})
								},1000)
							}
						}).then((r)=>{
               loginIn({username:this.option.username,password:this.from.confirmPassword}).then((res)=>{
                 if (res.code === 200){
                   uni.setStorageSync('token',res.token)
				   uni.setStorageSync("userId", res.userId);
                   this.$refs.uToast.show({
                     type: 'success',
                     title: '成功主题(带图标)',
                     message: "注册成功,请完善信息",
                     iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
                   })
                 }else {
                   this.$refs.uToast.show({
                     type: 'error',
                     message: r.msg,
                     iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
                   })
                 }
               })
            })

					}
				} else {
					this.$refs.uNotify.show({
						top: 10,
						type: 'warning',
						message: this.message,
						duration: 1000 * 2,
						fontSize: 20,
						safeAreaInsetTop: true
					})
				}
			}
		},
		onLoad(option) {
			this.option = option
			console.log(this.option);
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: '注 册',
			});

			uni.setNavigationBarColor({
				backgroundColor: '#FFFFFF',
				frontColor: '#333'
			})
		}
	}
</script>

<style lang="scss" scoped>
	.user_INFO {
		padding: 50rpx 45rpx;

		.user_input {
			margin: 30rpx 0;
			border-bottom: 1px solid #f6f6f6;
		}

		.next {
			width: 80%;
			text-align: center;
			height: 100rpx;
			line-height: 100rpx;
			margin: 60rpx auto;
			background-color: #1989fa;
			color: #f6f6f6;
			border-radius: 8px;
		}
	}
</style>
